package sshFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CreateCsv {
	
	 /**
     * CSV�ļ����ɷ���
     * @param head �ļ�ͷ
     * @param dataList �����б�
     * @param outPutPath �ļ����·��
     * @param filename �ļ���
     * @return
     */
    public static File createCSVFile(List<Object> head, List<List<Object>> dataList,String outPutPath, String filename) {
 
        File csvFile = null;
        BufferedWriter csvWtriter = null;
        try {
            csvFile = new File(outPutPath + File.separator + filename + ".csv");
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();
 
            // GB2312ʹ��ȷ��ȡ�ָ���","
            csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                    csvFile), "UTF-8"), 1024);
            // д���ļ�ͷ��
            writeRow(head, csvWtriter);
 
            // д���ļ�����
            for (List<Object> row : dataList) {
                writeRow(row, csvWtriter);
            }
            csvWtriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                csvWtriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return csvFile;
    }
 
    /**
     * дһ�����ݷ���
     * @param row
     * @param csvWriter
     * @throws IOException
     */
    private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
        // д���ļ�ͷ��
        for (Object data : row) {
            StringBuffer sb = new StringBuffer();
            String rowStr = sb.append(data).append(",").toString();
            csvWriter.write(rowStr);
        }
        csvWriter.newLine();
    }
 
 
    
    
    
    public static void createCSVtool(String filepath) {
    	List<Object> exportData = new ArrayList<Object>();
        exportData.add("NEType");         
        exportData.add("NEName");
        exportData.add("IPaddress");
        List<List<Object>> datalist = new ArrayList<List<Object>>();
        File file=new File(filepath);
     
		try {
			
		InputStreamReader in = new InputStreamReader(new FileInputStream(file),"UTF-8");
			
		BufferedReader	bf = new BufferedReader(in);      
        String s=null;
        int count=0;
        while((s = bf.readLine())!=null){
        	count++;
        	 if(count>=12){      		 
        		 if(s!=null && !"".equals(s) && !s.startsWith("---")){
        			 List<Object> data=new ArrayList<Object>();
                     String[] sp = s.split("\\s+");
                     for (int i = 0; i < sp.length; i++) {          
                     	data.add(sp[i]);
         			}
                     datalist.add(data);
             	}
        	              
        	 }     	
        	
            }
        bf.close();   
        
        String path = "C:\\ash\\zipRelation\\";
        String fileName = "JSLSTNE";
       createCSVFile(exportData, datalist, path, fileName);
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
